package com.weeho.dongyong.service;

import com.alibaba.fastjson.JSON;
import com.weeho.dongyong.dao.VersionsDao;
import com.weeho.dongyong.entity.Version;
import com.weeho.dongyong.entity.Versions;
import com.weeho.dongyong.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/20.
 */
@Service
public class VersionsService {
    @Autowired
    private VersionsDao versionsDao;

    /**
     * 通过条件查询版本信息
     *
     * @param versions
     * @return
     */
    public List<Versions> selectByCondition(Versions versions) {
        return versionsDao.selectByCondition(versions);
    }

    public Result validateVersion(int osType, String versionNum, String installationId) {

        Boolean isNewest = false;
        Boolean isTooOld = false;
        Versions version = new Versions();
        version.setVersionsTypeId(osType);
        List<Versions> verList = this.selectByCondition(version);
        Versions ver = verList.get(0);
        if (ver != null) {
            // validate param
            Version serviceOldVer = Version.createVersion(ver.getVersionNum());
            if (serviceOldVer == null) {
                Map<String,Object> mainMap = new HashMap<>();
                mainMap.put("oper_code",0);
                return Result.fail(JSON.toJSONString(mainMap));
            }
            Version serviceNewVer = Version.createVersion(ver.getLatestVersionNum());
            if (serviceNewVer == null) {
                Map<String,Object> mainMap = new HashMap<>();
                mainMap.put("oper_code",0);
                return Result.fail(JSON.toJSONString(mainMap));
            }
            Version clientVer = Version.createVersion(versionNum);
            if (clientVer == null) {
                Map<String,Object> mainMap = new HashMap<>();
                mainMap.put("oper_code",0);
                return Result.fail(JSON.toJSONString(mainMap));
            }

            int comOldValue = Version.compare(clientVer, serviceOldVer);
            int comNewValue = Version.compare(clientVer, serviceNewVer);

            if (comOldValue < 0) {
                isNewest = false;
                isTooOld = true;
            } else if (comNewValue < 0) {
                isNewest = false;
                isTooOld = false;
            } else {
                isNewest = true;
                isTooOld = false;
            }
            HashMap<String, String> map = new HashMap<>();
            map.put("isLatest", isNewest.toString());
            map.put("isTooOld", isTooOld.toString());
            map.put("url", ver.getLatestURL());
            map.put("version", ver.getLatestVersionNum());
            map.put("latestVersionInfo", ver.getLatestVersionInfo());
            map.put("size", ver.getSizeText());
            map.put("forceUpdateTip", ver.getForceUpdateTip());
            map.put("latestURL",ver.getLatestURL());
            map.put("latestNum",ver.getLatestVersionNum());

            return Result.ok(null,map);

        } else {
            Map<String,Object> mainMap = new HashMap<>();
            mainMap.put("oper_code",0);
            return Result.fail("error");
        }
    }


}
